﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Revis.VClinicNet.App.Internal;
using Revis.VClinicNet.App.Internal.Dto;
using Revis.VClinicNet.App.Internal.Dto.Validation;
using Revis.VClinicNet.UI.Internal.Mvc.Extensions;
using Revis.VClinicNet.UI.Internal.Mvc.Models.Forms;
using Revis.VClinicNet.UI.Internal.Mvc.Models.Pages;
using Revis.VClinicNet.Core.Domain.Credentials;
using Revis.VClinicNet.UI.Internal.Mvc.Infrastructure;

namespace Revis.VClinicNet.UI.Internal.Mvc.Controllers
{
    public class PatientController : Infrastructure.ControllerBase
    {
        private IPatientService _patientService = null;
        private IDoctorService _doctorService = null;

        public PatientController(IPatientService ips, IDoctorService ids)
        {
            _patientService = ips;
            _doctorService = ids;
        }

        #region Actions

        [HttpGet]
        [HistoryBackLinked]
        [AuthorizeFor(Role = WorkerUserRole.Administrator | WorkerUserRole.Recepcionist)]
        public ActionResult IndexR()
        {
            return View();
        }

        [HttpGet]
        [HistoryBackLinked]
        [AuthorizeFor(Role = WorkerUserRole.Administrator | WorkerUserRole.Doctor)]
        public ActionResult IndexD()
        {
            return View();
        }


        [HttpGet]
        [HistoryBackLinked]
        [AuthorizeFor(Role = WorkerUserRole.Administrator | WorkerUserRole.Recepcionist)]
        public ActionResult Create()
        {
            IList<DoctorInfoDto> doctors = null;
            PatientCreateDto patientModel = new PatientCreateDto();
            try {
                doctors = _doctorService.ListAll();
                patientModel.SetDoctorList(ViewData, doctors);
            } catch (ClinicException ce) {
                return View("Error", new HandleErrorInfo(ce, "Patient", "Create"));
            }
            return View(patientModel);
        }

        [HttpPost]
        [HistoryBackLinked(Type = BackLinkType.ModifyForm)]
        [AuthorizeFor(Role = WorkerUserRole.Administrator | WorkerUserRole.Recepcionist)]
        public ActionResult Create(PatientCreateDto patientModel)
        {
            long id = -1;
            try {
                id = _patientService.Create(patientModel);
            } catch (ValidationException ve) {
                ModelState.AddModelErrors(ve);
            } catch (ClinicException ce) {
                ModelState.AddErrorMessage(ce.Message);
            }
            if (ModelState.IsValid == false) {
                try {
                    patientModel.SetDoctorList(ViewData, _doctorService.ListAll());
                } catch (ClinicException ce) {
                    return View("Error", new HandleErrorInfo(ce, "Patient", "Create"));
                }
                return View(patientModel);
            }
            return RedirectToAction("Detail", new { id = id });
        }

        [HttpGet]
        [HistoryBackLinked]
        [AuthorizeFor(Role = WorkerUserRole.Administrator | WorkerUserRole.Recepcionist)]
        public ActionResult Edit(long id)
        {
            PatientEditDto patientModel = null;
            try {
                patientModel = _patientService.Find<PatientEditDto>(id);
                patientModel.SetDoctorList(ViewData, _doctorService.ListAll());
            } catch (ClinicException ce) {
                return View("Error", new HandleErrorInfo(ce, "Patient", "Create"));
            }
            return View(patientModel);
        }

        [HttpPost]
        [HistoryBackLinked(Type = BackLinkType.ModifyForm)]
        [AuthorizeFor(Role = WorkerUserRole.Administrator | WorkerUserRole.Recepcionist)]
        public ActionResult Edit(PatientEditDto patientModel)
        {
            long id = -1;
            try {
                id = _patientService.Edit(patientModel);
            } catch (ValidationException ve) {
                ModelState.AddModelErrors(ve);
            } catch (ClinicException ce) {
                ModelState.AddErrorMessage(ce.Message);
            }
            if (ModelState.IsValid == false) {
                try {
                    patientModel.SetDoctorList(ViewData, _doctorService.ListAll());
                } catch (ClinicException ce) {
                    return View("Error", new HandleErrorInfo(ce, "Patient", "Edit"));
                }
                return View(patientModel);
            }
            return RedirectToAction("Detail", new { id = id });
        }

        [HttpGet]
        [HistoryBackLinked]
        [AuthorizeFor(Role = WorkerUserRole.Administrator | WorkerUserRole.Doctor | WorkerUserRole.Recepcionist)]
        public ActionResult Detail(long id)
        {
            PatientDto patient = null;
            try {
                patient = _patientService.Find<PatientDto>(id);
            } catch (ClinicException ce) {
                return View("Error", new HandleErrorInfo(ce, "Patient", "Create"));
            }
            return View(patient);
        }

        [HttpPost]
        [AuthorizeFor(Role = WorkerUserRole.Administrator | WorkerUserRole.Doctor | WorkerUserRole.Recepcionist)]
        public JsonResult JsonSearch(string field, string value)
        {
            IList<PatientInfoDto> patients = null;
            try {
                patients = _patientService.FindBy<PatientInfoDto>(field, value);
            } catch (ClinicException) {
                return Json(new List<PatientInfoDto>());
            }
            return Json(patients, JsonRequestBehavior.AllowGet);
        }

        #endregion

    }
}
